package com.yongcheon.sms_manager.notification;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.widget.RemoteViews;

import com.yongcheon.sms_manager.R;
import com.yongcheon.sms_manager.activities.CancelNotifationActivity;
import com.yongcheon.sms_manager.activities.DeleteNotifationActivity;
import com.yongcheon.sms_manager.activities.MainActivity;
import com.yongcheon.sms_manager.receiver.sms.SmsReceiver;
import com.yongcheon.sms_manager.utils.AndroidSystemUtil;
import com.yongcheon.sms_manager.utils.DateUtil;
import com.yongcheon.sms_manager.vo.SMSVO;
import com.yongcheon.sms_manager.vo.SpamNumVO;
import com.yongcheon.sms_manager.vo.SpamSentenceVO;

/**
 * Created by yongcheon on 2014-12-18.
 */
public class SpamSentenceNotification {

    public void setNotification(Context context, SpamSentenceVO spamSentenceVO, SMSVO smsVO) {
        NotificationManager mNotiManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);

        Intent cancelIntent = new Intent(context.getApplicationContext(), CancelNotifationActivity.class);
        PendingIntent pCancelIntent = PendingIntent.getActivity(context.getApplicationContext(), SmsReceiver.NOTI_ID, cancelIntent, PendingIntent.FLAG_CANCEL_CURRENT );

        Intent deletelIntent = new Intent(context.getApplicationContext(), DeleteNotifationActivity.class);
        deletelIntent.putExtra(MainActivity.EXTRA_SPAM_ADDR, smsVO.getAddress());
//        deletelIntent.putExtra(MainActivity.EXTRA_SPAM_ID, String.valueOf(smsVO.get_Id()));
        deletelIntent.putExtra(MainActivity.EXTRA_SPAM_CATEGORY, context.getResources().getString(R.string.spam_sentence));
        deletelIntent.putExtra(MainActivity.EXTRA_SPAM_SUBJECT, context.getResources().getString(R.string.spam_sentence) +" : "+ spamSentenceVO.getSentence());

        PendingIntent pDeleteIntent = PendingIntent.getActivity(context.getApplicationContext(), SmsReceiver.NOTI_ID, deletelIntent, PendingIntent.FLAG_CANCEL_CURRENT );


        if(AndroidSystemUtil.ANDROID_VERSION < 16) {
            setNoti15(context, spamSentenceVO, smsVO, mNotiManager, pCancelIntent, pDeleteIntent);
        }else{
            setNoti16(context, spamSentenceVO, smsVO, mNotiManager, pCancelIntent, pDeleteIntent);
        }
    }

    public void setNoti15(Context context, SpamSentenceVO spamSentenceVO, SMSVO smsVO, NotificationManager mNotiManager, PendingIntent pIntent, PendingIntent pDeleteIntent){
        String title = context.getResources().getString(R.string.spam_sms_arrive) + "( "+ smsVO.getAddress() + " ) ";
        String contextText = " ( "+ context.getResources().getString(R.string.spam_sentence)+" : "+ spamSentenceVO.getSentence() + " ) "+smsVO.getBody();
        Notification notification = new Notification(R.drawable.ic_spam2, title, System.currentTimeMillis());
        notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
        notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE ;
        notification.setLatestEventInfo(context, title, contextText, pDeleteIntent);
        mNotiManager.notify(SmsReceiver.NOTI_ID, notification);
    }

    public void setNoti16(Context context, SpamSentenceVO spamSentenceVO, SMSVO smsVO, NotificationManager mNotiManager, PendingIntent pIntent, PendingIntent pDeleteIntent){

        Notification.Builder notiBuilder = new Notification.Builder(context.getApplicationContext())
                .setSmallIcon(R.drawable.ic_spam2)
                .setContentTitle(context.getResources().getString(R.string.spam_sms_arrive))
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_spam2))
                .setPriority(1)
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
//                .setContentIntent(pIntent)
                .addAction(R.drawable.ic_x, context.getResources().getString(R.string.cancel), pIntent)
                .addAction(R.drawable.ic_delete2, context.getResources().getString(R.string.spam_delete), pDeleteIntent)
                .setTicker(context.getResources().getString(R.string.spam_sms_arrive));

        Notification.InboxStyle inboxStyle = new Notification.InboxStyle(notiBuilder);
        inboxStyle.addLine(context.getResources().getString(R.string.spam_number)+" : "+smsVO.getAddress());
        inboxStyle.addLine(context.getResources().getString(R.string.spam_sentence)+" : "+spamSentenceVO.getSentence());
        inboxStyle.addLine(smsVO.getBody());
        Notification noti = inboxStyle.build();
        noti.flags |= Notification.FLAG_ONGOING_EVENT;
        noti.flags |= Notification.FLAG_NO_CLEAR;

        mNotiManager.notify(SmsReceiver.NOTI_ID, noti);

    }

    public void setNotiByView(Context context, SpamNumVO spamVO, SMSVO smsVO){
        RemoteViews napView = new RemoteViews(context.getPackageName(), R.layout.spam_notification);
        napView.setTextViewText(R.id.address, smsVO.getAddress());
        napView.setTextViewText(R.id.spam_type, spamVO.getCategory_2());
        napView.setTextViewText(R.id.title, smsVO.getBody());
        napView.setTextViewText(R.id.date, DateUtil.formatTimeStampString(context, smsVO.getDate(), false));
        NotificationManager mNotiManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        Notification noti = new Notification.Builder(context.getApplicationContext())
                .setSmallIcon(R.drawable.ic_spam2)
                .setContentTitle(context.getResources().getString(R.string.spam_sms_arrive))
                .setTicker(context.getResources().getString(R.string.spam_sms_arrive))
                .setContent(napView)
                .build();
        mNotiManager.notify(SmsReceiver.NOTI_ID, noti);
    }
}
